package com.sangmo.fts.process.storage.service;

import com.sangmo.boot.framework.common.session.UserSession;
import com.sangmo.boot.framework.plugin.dataview.crud.service.BaseViewService;
import com.sangmo.boot.framework.plugin.dataview.data.IDataViewRepo;
import com.sangmo.fts.common.authority.service.AuthorityEntityService;
import com.sangmo.fts.process.storage.interfaces.CsfOutboundCreation;
import com.sangmo.fts.process.storage.model.CsfAgreement;
import com.sangmo.fts.process.storage.model.CsfLadingBill;
import com.sangmo.fts.process.storage.model.CsfLoan;
import com.sangmo.fts.process.storage.model.CsfOutbound;
import com.sangmo.fts.process.storage.repo.CsfOutboundRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CsfOutboundEntityService extends AuthorityEntityService<CsfOutbound> {

    @Autowired
    private CsfOutboundRepo csfOutboundRepo;

    @Autowired
    private CsfLadingBillService csfLadingBillService;

    @Autowired
    private CsfLoanService csfLoanService;

    @Autowired
    private CsfAgreementService csfAgreementService;

    @Override
    protected IDataViewRepo<CsfOutbound> getRepo() {
        return csfOutboundRepo;
    }

    @Override
    protected void onCreate(CsfOutbound entity, Object bo) {
        super.onCreate(entity, bo);
        CsfOutboundCreation creation = (CsfOutboundCreation) bo;
        CsfLadingBill ladingBill = csfLadingBillService.get(creation.getBillId());
        entity.setLoanId(ladingBill.getLoanId());
        CsfLoan loan = csfLoanService.get(ladingBill.getLoanId());
        entity.setContractId(loan.getContractId());
        CsfAgreement agreement = csfAgreementService.get(loan.getAgreementId());
        entity.setShipper(agreement.getStorageId());
        entity.setConsignee(agreement.getBuyerId());
//        entity.setOutDate(ladingBill.getPickDate());
        entity.setConfirmedBy((Long) UserSession.getUserInterface().getId());
    }
}
